package smashed;

 

// *************************************************************************
   
public class CycleWorkSpace implements  WorkSpace { 
	public boolean AnyCycles;

	public int     counter;
 
	public static final int WHITE = 0;
 
	public static final int GRAY  = 1;
 
	public static final int BLACK = 2;
             
	public CycleWorkSpace() {
        AnyCycles = false;
        counter   = 0;
    }

	public void init_vertex( Vertex v ) 
      {
        v.VertexCycle = Integer.MAX_VALUE;
		//v.VertexCycle = Integer.MIN_VALUE;
        v.VertexColor = WHITE; // initialize to white color
    }

	public void preVisitAction( Vertex v ) {
        
        // This assigns the values on the way in
		if ( v.visited!=true )
		//if ( v.visited ) 
                { // if it has not been visited then set the
            // VertexCycle accordingly
            v.VertexCycle = counter++;
            v.VertexColor = GRAY; // we make the vertex gray
        }
    }


	public void postVisitAction( Vertex v ) 
      {
        // we are done with the visiting so make it black        
        v.VertexColor = BLACK;
		//v.VertexColor = WHITE;
        counter--;
    }


	public void checkNeighborAction( Vertex vsource, 
                     Vertex vtarget ) 
      {
        // if the graph is directed is enough to check that the source node
        // is gray and the adyacent is gray also to find a cycle
        // if the graph is undirected we need to check that the adyacent is not
        // the father, if it is the father the difference in the VertexCount is
        // only one.                                   
        //#if DIRECTED___
            if ( ( vsource.VertexColor == GRAY ) && ( vtarget.VertexColor == GRAY ) )
            //if ( ( vsource.VertexColor == GRAY ) || ( vtarget.VertexColor == GRAY ) )
               {
                AnyCycles = true;
            }
        //#elif UNDIRECTED___
//@            // undirected case
//@            if ( ( vsource.VertexColor == GRAY ) && 
//@                   ( vtarget.VertexColor == GRAY ) &&
//@                    vsource.VertexCycle != vtarget.VertexCycle+1 ) {
//@                AnyCycles = true;
//@            }
        //#endif    
        
    } // of checkNeighboor

	public void nextRegionAction(Vertex v) {
		// TODO Auto-generated method stub
		
	} 
}
